package com.then.architecture;


import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.ViewModelProvider;
import android.os.Bundle;

import com.luojilab.component.componentlib.router.Router;
import com.luojilab.component.componentlib.router.ui.UIRouter;
import com.luojilab.component.componentlib.service.AutowiredService;
import com.luojilab.componentservice.prefs.Preferences;
import com.then.architecture.mvp.databind.DataBindActivity;
import com.then.architecture.mvp.delegate.AppDelegate;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

/**
 * Created by 42524 on 2018/1/15.
 */

public abstract class BaseActivity<T extends AppDelegate> extends DataBindActivity<T> implements BuildConfigCallback, LifecycleOwner {

    @Inject
    ViewModelProvider.Factory viewModelFactory;

    @Inject
    Preferences preferences;

    private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);


    protected Router router = Router.getInstance();

    protected UIRouter uiRouter = UIRouter.getInstance();

    protected ViewModelProvider.Factory getViewModelFactory() {
        return viewModelFactory;
    }

    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AutowiredService.Factory.getInstance().create().autowire(this);
        super.onCreate(savedInstanceState);
    }

    protected void registEventbus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    protected void unRegistEventbus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


    public Preferences getPreferences() {
        return preferences;
    }
}
